package edu.tjpu.share.task;

import java.util.HashMap;

import org.phprpc.PHPRPC_Client;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import edu.tjpu.share.app.Fields;
import edu.tjpu.share.biz.IUploadFile;
import edu.tjpu.share.newui.UserList;
import edu.tjpu.share.po.FileForUpload;
import edu.tjpu.share.po.Server;
import edu.tjpu.share.service.UploadService;
import edu.tjpu.share.util.FileUploadClient;

public class UploadTask extends
		AsyncTask<HashMap<String, Object>, Integer, HashMap<String, Object>> {
	
	private FileForUpload file;
	private int notification_id;

	@Override
	protected HashMap<String, Object> doInBackground(
			HashMap<String, Object>... maps) {

		int port = -1;
		boolean withoutFile = (Boolean) maps[0].get(Fields.WITHOUTFILE);
		Handler uploadHandler = (Handler) maps[0].get("uploadHandler");
		Handler mUploadThreadHandler = (Handler) maps[0].get("mUploadThreadHandler");
		Server serverinfo = (Server) maps[0].get(Fields.SERVER_INFO);
		file = (FileForUpload) maps[0].get(Fields.FILE_FORUPLOAD);
		notification_id = (Integer)maps[0].get("notification_id");

		StringBuilder sbhost = new StringBuilder();
		sbhost.append("http://");
		sbhost.append(serverinfo.getHost());
		sbhost.append(":");
		sbhost.append(serverinfo.getProt());
		sbhost.append("/ShareServer/servlet/UploadFromAndroid");
		try {
			PHPRPC_Client uploadClient = new PHPRPC_Client(sbhost.toString());
			IUploadFile uploadFile = (IUploadFile) uploadClient
					.useService(IUploadFile.class);
			port = uploadFile.doUploadFile(file, withoutFile);
		} catch (Exception e) {
			Bundle bundle = new Bundle();
			bundle.putInt("notification_id", notification_id);
			bundle.putString("fileName", file.getFname());
			Message msg = new Message();
			msg.what = Fields.NETWORK_CONNECTION_ERROR;
			msg.setData(bundle);
			uploadHandler.sendMessage(msg);
			mUploadThreadHandler.sendEmptyMessage(UploadService.UPLOAD_FAILED);
			return null;
		}

		boolean result = false;
		if (port != -1 && port != 0) {
			FileUploadClient client = new FileUploadClient(
					serverinfo.getHost(), port);
			result = client.doUpload(file.getFurl(), file.getFname(),uploadHandler ,notification_id);
		}

		HashMap<String, Object> outmap = new HashMap<String, Object>();
		outmap.put(Fields.PORT, port);
		outmap.put(Fields.HANDLER, mUploadThreadHandler);
		outmap.put("uploadHandler", uploadHandler);
		outmap.put(Fields.STATUS, result);

		return outmap;
	}

	@Override
	protected void onPostExecute(HashMap<String, Object> result) {
		super.onPostExecute(result);
		if (isCancelled()) {
			result = null;
		}
		if (result != null) {
			boolean flag = (Boolean) result.get(Fields.STATUS);
			int port = (Integer) result.get(Fields.PORT);
			Handler mUploadThreadHandler = (Handler) result.get(Fields.HANDLER);
			Handler uploadHandler = (Handler) result.get("uploadHandler");
			Message msg = uploadHandler.obtainMessage();
			Bundle bundle = new Bundle();
			bundle.putInt("notification_id", notification_id);
			bundle.putString("fileName", file.getFname());
			msg.setData(bundle);
			if (port == -1)
				UserList.setStatus(false);
			else
				UserList.setStatus(true);
			if (flag) {
				msg.what = UploadService.UPLOAD_COMPLETE;
			} else if (!flag && port == 0) {
				msg.what = UploadService.NOTIFY_COMPLETE;
			} else {
				msg.what = UploadService.UPLOAD_FAILED;
			}
			uploadHandler.sendMessage(msg);
			mUploadThreadHandler.sendEmptyMessage(UploadService.UPLOAD_COMPLETE);
		}
	}
}
